package com.yequan.hbb.view.fragment.loginfragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yequan.hbb.R;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.view.LoginActivity;

import cn.smssdk.SMSSDK;

/**
 * A simple {@link Fragment} subclass.
 */
public class CheckFragment extends Fragment implements View.OnClickListener {

    public final static int CHECK = 2;
    private TextView tip;
    private TextInputEditText phoneCode;
    private TextView codeTime;
    private Button next;
    private String messageCode;
    private static String userPhone;
    private static final int CODE_ING = 1;   //已发送，倒计时
    private static final int CODE_REPEAT = 2;  //重新发送
    private static final int SMSDDK_HANDLER = 3;  //短信回调
    private int TIME = 60;//倒计时60s
    public static Handler handler;

    public CheckFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_check, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyApplication.level = CHECK;
        tip = (TextView) view.findViewById(R.id.check_text);
        tip.setText("已发送验证码短信至" + userPhone);
        phoneCode = (TextInputEditText) view.findViewById(R.id.message_code);
        codeTime = (TextView) view.findViewById(R.id.check_time);
        next = (Button) view.findViewById(R.id.check_next);
        phoneCode.setHint("请输入验证码");
        codeTime.setEnabled(false);
        codeTime.setOnClickListener(this);
        next.setOnClickListener(this);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case CODE_ING:
                        codeTime.setText("已发送" + --TIME + "s后重试");
                        break;
                    case CODE_REPEAT:
                        codeTime.setText("重新发送");
//                        codeTime.setTextColor(R.color.white);
                        codeTime.setBackgroundResource(R.drawable.login_button_bac);
                        codeTime.setEnabled(true);
                        break;
                    case SMSDDK_HANDLER:
                        int event = msg.arg1;
                        int result = msg.arg2;
                        Object data = msg.obj;
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                                Toast.makeText(getContext(), "验证成功", Toast.LENGTH_SHORT).show();
                                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.login_container, LoginActivity.passwordFragment).commit();
                                LoginActivity.loginTitle.setText("设置密码");
                            } else if (result == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                                Toast.makeText(getContext(), "验证码已发送", Toast.LENGTH_SHORT).show();
                            }
                        } else if (result == SMSSDK.RESULT_ERROR) {
                            Toast.makeText(getContext(), "验证失败,请重试", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.check_time:

                break;
            case R.id.check_next:
                messageCode = phoneCode.getText().toString().trim();
                if ("".equals(messageCode)) {
                    phoneCode.setError("验证码不能为空");
                } else {
                    SMSSDK.submitVerificationCode("86", userPhone, messageCode);
                }
                break;
        }
    }

    public static class checkPhoneCodeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            userPhone = intent.getStringExtra("userPhone");
            Log.i("kkkkk", "onReceive: "+userPhone);
            setResultData(userPhone);
        }
    }
}
